import { Component } from "@xuekl/cli-core/enums"
import * as c from "@xuekl/cli-core/components"
import { BaseForm } from "@xuekl/cli-core/builder"
import { treeData } from "_/utils"

export default class Menu extends BaseForm {
    menuId = ''
    parentId = 0
    menuType = 'C'
    menuName = ''
    orderNum = 0
    path = ''
    component = ''
    perms = ''
    isFrame = '1'
    isCache = '0'
    visible = '0'
    status = '0'

    clear(): void {
        this.setForm(new Menu(this.opts))
        this.resetFields()
    }

    getParentId(): c.TreeSelect {
        return {
            type: Component.TreeSelect,
            label: '上级菜单',
            span: 24,
            config: {
                url: '/system/menu/list',
                valueTarget: 'menuId',
                labelTarget: 'menuName',
                convertData: (data) => {
                    const result = [{
                        menuId: 0,
                        menuName: '主类目',
                        children: treeData(data, 'menuId', 'parentId')
                    }]
                    return result
                }
            },
            element: {
                checkStrictly: true
            }
        }
    }

    getMenuType(): c.Radio {
        return {
            type: Component.Radio,
            label: '菜单类型',
            span: 24,
            config: {
                dict: 'sys_menu_type'
            }
        }
    }

    getMenuName(): c.Input {
        return {
            type: Component.Input,
            label: '菜单名称',
            mode: 'query',
            required: true,
            element: {
                maxlength: 20,
            }
        }
    }

    getOrderNum(): c.InputNumber {
        return {
            type: Component.InputNumber,
            label: '显示排序',
        }
    }

    getPath(): c.Input {
        return {
            type: Component.Input,
            label: '路由地址',
            required: true,
            show: () => 'M,C'.includes(this.menuType),
            element: {
                maxlength: 100,
            }
        }
    }

    getComponent(): c.Input {
        return {
            type: Component.Input,
            label: '组件路径',
            show: () => this.menuType === 'C',
            element: {
                minlength: 100,
            }
        }
    }

    getPerms(): c.Input {
        return {
            type: Component.Input,
            label: '权限字符',
            show: () => 'C,F'.includes(this.menuType),
            element: {
                minlength: 50,
            }
        }
    }

    getIsFrame(): c.Radio {
        return {
            type: Component.Radio,
            label: '是否外链',
            show: () => this.menuType === 'C',
            config: {
                dict: 'sys_normal_disable'
            }
        }
    }

    getIsCache(): c.Radio {
        return {
            type: Component.Radio,
            label: '是否缓存',
            show: () => this.menuType === 'C',
            config: {
                dict: 'sys_normal_disable'
            }
        }
    }

    getVisible(): c.Radio {
        return {
            type: Component.Radio,
            label: '显示状态',
            config: {
                dict: 'sys_normal_disable'
            }
        }
    }

    getStatus(): c.Radio {
        return {
            type: Component.Radio,
            label: '菜单状态',
            config: {
                dict: 'sys_normal_disable'
            }
        }
    }
}
